<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <p mv-text="msg"></p>
        <p mv-text="msg"></p>
        <hr>
        <p mv-text="info"></p>
        <p mv-text="info"></p>
    </div>
</body>
<script>
    function Mvvm(obj) {
        this.root = document.querySelector(obj.el);
        this._data = obj.data;
        for (let key in this._data) {
            let sel = "[mv-text=" + key + "]";
            // console.log(sel);
            let arr = this.root.querySelectorAll(sel);
            // console.log(arr);
            for (let index in arr) {
                arr[index].textContent = this._data[key];
            }
            Object.defineProperty(this, key, {
                get() {
                    console.log(key, "get方法!");
                    return this._data[key];
                },
                set(nv) {
                    console.log(key, "set方法!");
                    this._data[key] = nv;
                    for (let index in arr) {
                        arr[index].textContent = this._data[key];
                    }
                }
            })
        }
    }
    var mv = new Mvvm({
        el: "#app",
        data: {
            msg: "msg",
            info: "info"
        }
    })
</script>

</html>